package day38.edu;

import java.util.*;

// Created: 08/10/2025, ChangJiang Ru
public class DAO <T>{
    Map<String, T> map = new HashMap<>();

    public void save(String id, T entity)
    {
        if(!map.containsKey(id))
            map.put(id, entity);
    }

    public T get(String id)
    {
        return map.get(id);
    }

    public void update(String id, T entity)
    {
        if(map.containsKey(id))
            map.put(id, entity);
    }

    public List<T> list()
    {
        /*
        Collection<T> values = map.values();
        ArrayList<T> list = new ArrayList<>();
        list.addAll(values);
        */

        Collection<T> values = map.values();
        ArrayList<T> list = new ArrayList<>(values);

        return list;
    }

    public void delete(String id)
    {
        map.remove(id);
    }
}
